//
//  AppDelegate.m
//  Shine
//
//  Created by 王斌 on 16/3/9.
//  Copyright © 2016年 王斌. All rights reserved.
//

#import "AppDelegate.h"
#import "MineViewController.h"
#import "AnalyseViewController.h"
#import "RunViewController.h"
#import "FindViewController.h"
#import "TalkViewController.h"
#import "CoreDataManager.h"
#import "EaseUI.h"
@interface AppDelegate ()


@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    /// 环信
    //registerSDKWithAppKey:注册的appKey，详细见下面注释。
    //apnsCertName:推送证书名(不需要加后缀)，详细见下面注释。
    [[EaseMob sharedInstance] registerSDKWithAppKey:@"wangbin522#run" apnsCertName:@"bin"];
    [[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    //iOS8 注册APNS
    if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) {
        [application registerForRemoteNotifications];
        UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge |
        UIUserNotificationTypeSound |
        UIUserNotificationTypeAlert;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
        [application registerUserNotificationSettings:settings];
    }
    else{
        UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeBadge |
        UIRemoteNotificationTypeSound |
        UIRemoteNotificationTypeAlert;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
    }
    [[EaseSDKHelper shareHelper] easemobApplication:application
                      didFinishLaunchingWithOptions:launchOptions
                                             appkey:@"wangbin522#run"
                                       apnsCertName:@"bin"
                                        otherConfig:@{kSDKConfigEnableConsoleLogger:[NSNumber numberWithBool:YES]}];
    
    
    BMKMapManager *map = [[BMKMapManager alloc] init];
    BOOL ret = [map start:@"fBXhxFcwp7q3Fq7NY1DXIhtF" generalDelegate:nil];
    if (!ret) {
        NSLog(@"百度地图启动失败");
    }
    
    CoreDataManager *manager = [CoreDataManager shareInstance];
    [NSEntityDescription insertNewObjectForEntityForName:@"HealthKit" inManagedObjectContext:manager.managedObjectContext];
    [manager saveContext];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];
    
    MineViewController *mineVC = [[MineViewController alloc] init];
    UINavigationController *mineNAVC = [[UINavigationController alloc] initWithRootViewController:mineVC];
    mineNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[[UIImage imageNamed:@"iconfont-wo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"iconfont-wo-2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    AnalyseViewController *analyseVC = [[AnalyseViewController alloc] init];
    UINavigationController *analyseNAVC = [[UINavigationController alloc] initWithRootViewController:analyseVC];
    analyseNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"分析" image:[[UIImage imageNamed:@"iconfont-periodicanalysis"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"iconfont-periodicanalysis-2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    RunViewController *runVC = [RunViewController shareRunViewController];
    UINavigationController *runNAVC = [[UINavigationController alloc] initWithRootViewController:runVC];
    runNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:[[UIImage imageNamed:@"activity_a_off@2x_n"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"activity_a_off@2x_h"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

    FindViewController *findVC = [[FindViewController alloc] init];
    UINavigationController *findNAVC = [[UINavigationController alloc] initWithRootViewController:findVC];
    findNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[[UIImage imageNamed:@"iconfont-faxian"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"iconfont-faxian-2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    TalkViewController *talkVC = [[TalkViewController alloc] init];
    UINavigationController *talkNAVC = [[UINavigationController alloc] initWithRootViewController:talkVC];
    talkNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"聊天" image:[[UIImage imageNamed:@"iconfont-liaotian"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"iconfont-liaotian-2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    UITabBarController *tabbarVC = [[UITabBarController alloc] init];
    tabbarVC.viewControllers = @[mineNAVC, analyseNAVC, runNAVC, findNAVC, talkNAVC];
    tabbarVC.selectedIndex = 2;
    self.window.rootViewController = tabbarVC;
    tabbarVC.tabBar.translucent = NO;
    tabbarVC.tabBar.tintColor = COLORYELLO;
    [[UITabBar appearance ]setBarTintColor:[UIColor colorWithRed:0.1333 green:0.1451 blue:0.1569 alpha:1.0]];
    // Override point for customization after application launch.
    [ReadData shareInstance];
    [ReadData deviceVersion];
     return YES;
}
// 将得到的deviceToken传给SDK
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    [[EaseMob sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

// 注册deviceToken失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    [[EaseMob sharedInstance] application:application didFailToRegisterForRemoteNotificationsWithError:error];
    NSLog(@"error -- %@",error);
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    [[ReadData shareInstance] readDataFromHealth];
}

/// 将要到后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [[EaseMob sharedInstance] applicationDidEnterBackground:application];

}


/// 将要回到前台
- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    RunViewController *runVC = [RunViewController shareRunViewController];
    [runVC searchCount];
    [[EaseMob sharedInstance] applicationWillEnterForeground:application];

}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

/// 申请处理时间
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
    [[EaseMob sharedInstance] applicationWillTerminate:application];
}



@end
